package com.scau.dunka.controller;


import com.easyFramework.annotation.Autowired;
import com.easyFramework.annotation.Controller;
import com.easyFramework.annotation.RequestMapping;
import com.easyFramework.annotation.RequestParam;
import com.easyFramework.bean.ModelAndView;
import com.scau.dunka.bean.Article;
import com.scau.dunka.bean.Category;
import com.scau.dunka.service.ArticleService;
import com.scau.dunka.service.UserService;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 *  Dunka 19-1-10
 *  用处：控制层
 */
@Controller
@RequestMapping("/")
public class UserController {

    @Autowired
    UserService userService;
    @Autowired
    ArticleService articleService;

    /*
        游客浏览博客首页
     */
    @RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addModel("articles",articleService.getFirst10Article());
        modelAndView.setPath("views/index.jsp");
        return modelAndView;
    }

    @RequestMapping("/admin/index")
    public ModelAndView admin(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addModel("articles",articleService.getFirst10Article());
        modelAndView.setPath("admin/index.jsp");
        return modelAndView;
    }

    /*
    *   管理员登录
    */
    @RequestMapping(value = "/admin/login")
    public ModelAndView adminLogin() {
        ModelAndView modelAndView =new ModelAndView("admin/login.jsp");
        return modelAndView;
    }

    @RequestMapping(value = "/do_login",method = "post")
    public ModelAndView dologin(@RequestParam("username")String username,
                                @RequestParam("password")String password, HttpServletRequest request){
       ModelAndView modelAndView = new ModelAndView();
       if(userService.login(username,password)){
            request.getSession().setAttribute("username",username);
            request.getSession().setAttribute("password",password);
            modelAndView.addModel("username",username);
            modelAndView.addModel("password",password);
            modelAndView.addModel("articles",articleService.getFirst10Article());
            modelAndView.setPath("admin/index.jsp");
        } else{
          modelAndView.addModel("error","用户名或密码错误");
          modelAndView.setPath("admin/login.jsp");
        }
        return modelAndView;
    }

    /*
     * get方法跳转到写博文的面板
     */
    @RequestMapping(value = "/write")
    public ModelAndView write(HttpServletRequest request){
        ModelAndView modelAndView =new ModelAndView();
        List<Category> categories = articleService.getCategories();
        categories.remove(0);
        modelAndView.addModel("categories",categories);
        request.getSession().setAttribute("categories",categories);
        modelAndView.setPath("admin/write.jsp");
        return modelAndView;
    }

    /*
     * @return 写完博文之后提交或者修改博文之后提交
     */
    @RequestMapping(value = "/write", method = "post")
    public ModelAndView write(@RequestParam("title")String title,
                              @RequestParam(value = "id",required = false)Long id,
                              @RequestParam("content")String content,
                              @RequestParam("category")String display, HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();

        Article article = new Article();
        article.setId(id);
        article.setTitle(title);
        article.setContent(content);
        List<Category> categories = articleService.getCategories();
        long numID=0;
        for(Category category:categories){
            if(category.getName().equals(display))
                numID = category.getId();
        }
        article.setCategoryId(numID);

        articleService.insertOrUpdateBlog(article);
        //设置为null是因为修改之后的实体不会一直存在，这样在之后点开写博文的按钮就不会再次有原来修改的博文
        request.getSession().setAttribute("article",null);

        modelAndView.addModel("articles",articleService.getFirst10Article());
        modelAndView.setPath("/admin/index.jsp");
        return modelAndView;
    }

    /*
        通过博文id删除博文并跳转回管理页面
     */
    @RequestMapping("/delete")
    public ModelAndView delete(@RequestParam("id") Long id) {
        ModelAndView modelAndView  = new ModelAndView();
        System.out.println(id);
        articleService.deleteArticleById(id);
        modelAndView.addModel("articles",articleService.getFirst10Article());
        modelAndView.setPath("admin/index.jsp");
        return modelAndView;
    }

    /*
        通过博文id修改博文并跳转回管理页面
     */
    @RequestMapping("/update")
    public ModelAndView update(@RequestParam("id") long id,HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        System.out.println(id);
        Article article = articleService.getArticleById(id);
        modelAndView.addModel("article",article);
        request.getSession().setAttribute("id",id);
        request.getSession().setAttribute("article",article);
        modelAndView.setPath("admin/write.jsp");
        return modelAndView;
    }

    /*
        通过选择首页上方的分类名，显示出该分类里的博文的标题和摘要，可通过点击阅读全文进行全文阅览
     */
    @RequestMapping("/column")
    public ModelAndView column(HttpServletRequest request) {
        ModelAndView modelAndView = new ModelAndView();
        String name = request.getParameter("page");
        modelAndView.addModel("articles", articleService.getArticlesByCategoryName(name));
        modelAndView.setPath("views/columnPage.jsp");
        return modelAndView;
    }

    /*
        通过点击阅读全文进行全文阅览
     */
    @RequestMapping("/detail/firstPage")
    public ModelAndView detail(@RequestParam("id") Long id) {
        Article article = articleService.getArticleById(id);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addModel("article", article);
        modelAndView.setPath("views/detail.jsp");
        return modelAndView;
    }

}
